<?php
/**
 * Created by 784855684@qq.com.
 * Link: https://github.com/lichtung/wshore
 * User: linzh
 * Date: 6/30/17
 * Time: 3:32 PM
 */
declare(strict_types=1);


namespace application\module\admin\addon\sign\test;


use wshore\library\Wetest;

class UserModelTest extends Wetest
{

    /**
     * 测试密码加密
     * sha1长度40,md5长度32
     * @return void
     */
    public function testencrypt()
    {
        $this->assertEquals(strlen(sha1('123456')), 40);
        $this->assertEquals(strlen(sha1('dsadsadasda')), 40);

        $this->assertEquals(strlen(md5('123456')), 32);
        $this->assertEquals(strlen(md5('dsadsadasda')), 32);
    }

    /**
     * 测试获取信息
     * @return void
     */
    public function testgetInfo()
    {
        $UserModel = new UserModel(1);
        $this->assertEquals('lzh', $UserModel->nickname);
        $UserModel = new UserModel('admin');
        $this->assertEquals('lzh', $UserModel->nickname);
        $UserModel = new UserModel('784855684@qq.com');
        $this->assertEquals('lzh', $UserModel->nickname);
    }

    /**
     * 测试密码断定
     * @return void
     */
    public function testpasswd()
    {
        $UserModel = new UserModel(1);
        # 无论是加密还是未加密的  都是正确的密码

        $this->assertTrue(Sign::getInstance()->isPasswordRight('123456'));
    }

    /**
     * 测试添加客户
     * @return UserModel
     */
    public function testAddUser()
    {
        $username = 'test' . time();
        $email = time() . '@qq.com';

        $user = new UserModel();

        $user->nickname = $user->username = $username;
        $user->email = $email;
        $user->password = '123456';

        $res = $user->save();
        # 保存成功
        $this->assertTrue($res);


        # 信息立即被添加
        $id = $user->id;
        $this->assertTrue(is_numeric($id));
        $this->assertEquals($username, $user->username);
        $this->assertEquals($email, $user->email);
        $this->assertEquals(Sign::encryptPassword('123456'), $user->password);

        # 再次new也正确
        $user2 = new UserModel($id);
        $this->assertEquals($username, $user2->username);
        $this->assertEquals($email, $user2->email);
        $this->assertEquals(Sign::encryptPassword('123456'), $user2->password);

        return $user;
    }

    /**
     * 测试删除客户
     * @depends testAddUser
     * @param UserModel $user
     * @return void
     */
    public function testRemove(UserModel $user)
    {
        # 有效？
        $id = $user->id;
        $this->assertTrue(is_numeric($id));

        # 删除
        $this->assertTrue($user->remove());

        $flag = false;
        try {
            new UserModel($id);
        } catch (RecordNotFoundException $e) {
            $this->assertTrue(true);
            $flag = true;
        } finally {
            $flag or $this->assertTrue(false);
        }

    }

    /**
     * 测试禁用客户登陆
     * @return void
     */
    public function testDisable()
    {
        $user = new UserModel(1);
        $user->disable = 1;
        $this->assertTrue($user->save());
        $this->assertTrue(1 === intval($user->disable));
        $user->disable = 0;
        $this->assertTrue($user->save());
        $this->assertTrue(0 === intval($user->disable));
    }
}